package com.ushine.web.template.transactional.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ushine.web.component.exception.ServiceException;
import com.ushine.web.component.exception.ServiceExceptionEnum;
import com.ushine.web.template.transactional.service.ISecondTransactionService;
import com.ushine.web.template.transactional.mapper.SecondTransactionMapper;
import com.ushine.web.template.transactional.model.SecondTransactionDO;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @Author: yls
 * @Date 2022/2/14 11:13
 * @Description: 模拟事务控制
 * @Version 1.0
 **/
@Service
public class SecondTransactionServiceImpl extends ServiceImpl<SecondTransactionMapper, SecondTransactionDO>
        implements ISecondTransactionService {
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void insert(SecondTransactionDO secondTransaction) {
        save(secondTransaction);
    }


    @Override
    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
    public void insertByException(SecondTransactionDO secondTransaction) throws ServiceException {
        save(secondTransaction);
        throw new ServiceException(ServiceExceptionEnum.NOT_SUPPORT_TYPE);
    }

    /**
     * 功能描述:
     * 〈 hibernate不支持NESTED类型的事务 〉
     *
     * @param secondTransaction
     * @return : void
     * @throws :
     * @author : yls
     * @date : 2022/3/8 10:34
     */
    @Override
    @Transactional(propagation = Propagation.NESTED, rollbackFor = Exception.class)
    public void insertNestedByException(SecondTransactionDO secondTransaction) throws ServiceException {
        save(secondTransaction);
        throw new ServiceException(ServiceExceptionEnum.NOT_SUPPORT_TYPE);
    }

    @Override
    @Transactional(readOnly = true)
    public List<SecondTransactionDO> listSecondTransactions() {
        return list();
    }
}
